/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import Handlers.CustomerHandler;
import Handlers.DistanceHandler;
import ObjectClass.Customer;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Jeppe
 */
public class OpretKunde extends javax.swing.JDialog {

    private Main main;
    private DistanceHandler distanceHandler;
    private CustomerHandler customerHandler;

    /**
     * Creates new form OpretKunde
     */
    public OpretKunde(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        main = (Main) parent;
        distanceHandler = main.getDistanceHandler();
        customerHandler = main.getCustomerHandler();
        setByCombo();
    }

    public void setByCombo() {
        ArrayList<String> cityList = distanceHandler.getAllCities();
        for (int i = 0; i < cityList.size(); i++) {
            cityCombo.addItem(cityList.get(i));
        }
    }

    private void clearFields() {
        tlftxt.setText("");
        nametxt.setText("");
        addresstxt.setText("");
        zipcodetxt.setText("");
        cityCombo.setSelectedIndex(0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tlftxt = new javax.swing.JTextField();
        nametxt = new javax.swing.JTextField();
        addresstxt = new javax.swing.JTextField();
        zipcodetxt = new javax.swing.JTextField();
        OpretKunde = new javax.swing.JButton();
        cityCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Opret Kunde");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setOpaque(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Tlf:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Navn:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Adresse:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Postnr | By:");

        tlftxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tlftxtFocusGained(evt);
            }
        });

        nametxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nametxtFocusGained(evt);
            }
        });

        addresstxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                addresstxtFocusGained(evt);
            }
        });

        zipcodetxt.setEditable(false);

        OpretKunde.setText("Opret Kunde");
        OpretKunde.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpretKundeActionPerformed(evt);
            }
        });

        cityCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cityComboItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(OpretKunde)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nametxt)
                            .addComponent(tlftxt, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addresstxt)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(zipcodetxt, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cityCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tlftxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nametxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(addresstxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(zipcodetxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cityCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OpretKunde)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 310, 170));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Pics/Baggrund 2.PNG"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(-5, 0, 340, 190));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OpretKundeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpretKundeActionPerformed
        ArrayList<String> fejlList = new ArrayList<>();
        String fejlBesked;
        fejlBesked = "Der er fejl i: ";
        if (tlftxt.getText().equals("") || !tlftxt.getText().matches("\\d+")) {
            fejlList.add("Tlf");
        }
        if (nametxt.getText().equals("")) {
            fejlList.add("Navn");
        }
        if (addresstxt.getText().equals("")) {
            fejlList.add("Adresse");
        }

        if (tlftxt.getText() != null || tlftxt.getText().equals("") || nametxt.getText() != null || nametxt.getText().equals("") || addresstxt.getText() != null || addresstxt.getText().equals("")) {
            for (int i = 0; i < fejlList.size(); i++) {
                if (fejlList.size() == 1) {
                    fejlBesked = fejlBesked + fejlList.get(i);
                } else if (i == fejlList.size() - 1) {
                    fejlBesked = fejlBesked + " og " + fejlList.get(i);
                } else if (i < fejlList.size()) {
                    fejlBesked = fejlBesked + " " + fejlList.get(i);
                }
            }
        }
        if (tlftxt.getText().matches("\\d+")) {
            if (customerHandler.tjekCustomerByPhoneNo(Integer.parseInt(tlftxt.getText()))) {
                JOptionPane.showMessageDialog(this, "Det indtastede tlf findes allerede i databasen.");
            }
        }
        if (tlftxt.getText() != null && !tlftxt.getText().equals("") && nametxt.getText() != null && !nametxt.getText().equals("") && addresstxt.getText() != null && !addresstxt.getText().equals("")) {
            Customer customer;
            customer = new Customer(Integer.parseInt(tlftxt.getText()), nametxt.getText(), addresstxt.getText(), zipcodetxt.getText(), (String) cityCombo.getSelectedItem());
            customerHandler.insertToDB(customer);
            clearFields();
            this.setVisible(false);
        } else {
            JOptionPane.showMessageDialog(this, fejlBesked);
        }
    }//GEN-LAST:event_OpretKundeActionPerformed

    private void cityComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cityComboItemStateChanged
        zipcodetxt.setText(distanceHandler.getZipcodeByCity((String) cityCombo.getSelectedItem()));
    }//GEN-LAST:event_cityComboItemStateChanged

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        tlftxt.setText(main.getCurCustomerPhone() + "");
        if(tlftxt.getText().equals("0")){
            tlftxt.setText("");
        }
    }//GEN-LAST:event_formComponentShown

    private void tlftxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tlftxtFocusGained
        tlftxt.selectAll();
    }//GEN-LAST:event_tlftxtFocusGained

    private void nametxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nametxtFocusGained
        nametxt.selectAll();
    }//GEN-LAST:event_nametxtFocusGained

    private void addresstxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_addresstxtFocusGained
        addresstxt.selectAll();
    }//GEN-LAST:event_addresstxtFocusGained

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OpretKunde.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OpretKunde.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OpretKunde.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OpretKunde.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                OpretKunde dialog = new OpretKunde(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton OpretKunde;
    private javax.swing.JTextField addresstxt;
    private javax.swing.JComboBox cityCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField nametxt;
    private javax.swing.JTextField tlftxt;
    private javax.swing.JTextField zipcodetxt;
    // End of variables declaration//GEN-END:variables
}
